package cn.drj.myutils.adapter.ViewHolder;

/**
 * Created by mk on 2017/11/7.
 */

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import cn.drj.myutils.adapter.DRJRecyclerViewAdapter;
import cn.drj.myutils.adapter.DRJViewHolderHelper;
import cn.drj.myutils.adapter.Listener;
import cn.drj.myutils.adapter.Listener.DRJOnRVItemClickListener;
import cn.drj.myutils.adapter.Listener.DRJOnRVItemLongClickListener;


public class DRJRecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
    protected Context mContext;
    protected DRJOnRVItemClickListener mOnRVItemClickListener;
    protected DRJOnRVItemLongClickListener mOnRVItemLongClickListener;
    protected DRJViewHolderHelper mViewHolderHelper;
    protected RecyclerView mRecyclerView;
    protected DRJRecyclerViewAdapter mRecyclerViewAdapter;

    public DRJRecyclerViewHolder(DRJRecyclerViewAdapter recyclerViewAdapter, RecyclerView recyclerView, View itemView, DRJOnRVItemClickListener onRVItemClickListener, DRJOnRVItemLongClickListener onRVItemLongClickListener) {
        super(itemView);
        mRecyclerViewAdapter = recyclerViewAdapter;
        mRecyclerView = recyclerView;
        mContext = mRecyclerView.getContext();
        mOnRVItemClickListener = onRVItemClickListener;
        mOnRVItemLongClickListener = onRVItemLongClickListener;
        itemView.setOnClickListener(new Listener.DRJOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (v.getId() == DRJRecyclerViewHolder.this.itemView.getId() && null != mOnRVItemClickListener) {
                    mOnRVItemClickListener.onRVItemClick(mRecyclerView, v, getAdapterPositionWrapper());
                }
            }
        });
        itemView.setOnLongClickListener(this);

        mViewHolderHelper = new DRJViewHolderHelper(mRecyclerView, this);
    }

    public DRJViewHolderHelper getViewHolderHelper() {
        return mViewHolderHelper;
    }

    @Override
    public boolean onLongClick(View v) {
        if (v.getId() == this.itemView.getId() && null != mOnRVItemLongClickListener) {
            return mOnRVItemLongClickListener.onRVItemLongClick(mRecyclerView, v, getAdapterPositionWrapper());
        }
        return false;
    }

    public int getAdapterPositionWrapper() {
        if (mRecyclerViewAdapter.getHeadersCount() > 0) {
            return getAdapterPosition() - mRecyclerViewAdapter.getHeadersCount();
        } else {
            return getAdapterPosition();
        }
    }
}
